﻿/**
* @description  Transitions a graphic from one color transform to another.
*/
package com.foed.animation.transitions {
	
	import flash.display.DisplayObject;
	import flash.geom.ColorTransform;

	public class ColorTransition extends Transition {
		
		/**
		* @description  Constructor.
		*
		* @param  pClip  The clip on which to perform the transition.
		* @param  pStartTransform  The starting values for the color transition.
		* @param  pEndTransform  The destination values for the color transition.
		* @param  pTime  The length in milliseconds for the transition.
		* @param  pType  The type of transition. IN moves from startTransform to endTransfom. OUT does the opposite.
		* @param  pEaseFunction  The function to use to transform the transition values over time.
		*/
		public function ColorTransition(
			pClip:DisplayObject=null,
			pStartTransform:ColorTransform=null,
			pEndTransform:ColorTransform=null,
			pTime:Number=1000,
			pType:String=null,
			pEaseFunction:Function=null
		) {
			pStartTransform = pStartTransform || new ColorTransform(0, 0, 0, 1, 255, 255, 255, 0);
			pEndTransform = pEndTransform || new ColorTransform();
			super(pClip, pTime, pStartTransform, pEndTransform, pType, pEaseFunction);
		}
	
		/**
		* @description  Turns a ColorTransform into a simple object for use with TweenRunner.
		*
		* @param  pTransform  The ColorTransform instance to make into a simple object.
		*/
		private function makeTransform(pTransform:ColorTransform):Object {
			var pObject:Object = {
				ra:pTransform.redMultiplier,
				rb:pTransform.redOffset,
				ga:pTransform.greenMultiplier,
				gb:pTransform.greenOffset,
				ba:pTransform.blueMultiplier,
				bb:pTransform.blueOffset,
				aa:pTransform.alphaMultiplier,
				ab:pTransform.alphaOffset
			};
			return pObject;
		}

		/**
		* @description  Called each iteration of the tween by the TweenRunner to transform values.
		*
		* @param  pChangeValues  The current values of the transition at each iteration of the tween.
		*/
		override protected function runTransition(pChangeValues:Object):void {
			clip.transform.colorTransform = new ColorTransform(
				pChangeValues.ra,
				pChangeValues.ga,
				pChangeValues.ba,
				pChangeValues.aa,
				pChangeValues.rb|0,
				pChangeValues.gb|0,
				pChangeValues.bb|0,
				pChangeValues.ab
			);
			super.runTransition(pChangeValues);
		}

		/**
		* @description  Removes any artifacts from the transition.
		*/
		override protected function cleanUp():void {
			clip.transform.colorTransform = new ColorTransform();
			super.cleanUp();
		}

		/**
		* @description  Sets the starting color transform for the transition.
		*
		* @param  pObject  The starting ColorTransform instance for the transition.
		*/
		override public function set startTransform(pObject:Object):void {
			super.startTransform = makeTransform(ColorTransform(pObject));
		}
	
		/**
		* @description  Sets the destination color transform for the transition.
		*
		* @param  pObject  The destination ColorTransform instance for the transition.
		*/
		override public function set endTransform(pObject:Object):void {
			super.endTransform = makeTransform(ColorTransform(pObject));
		}
		
	}

}